package com.aifeng.hassan_food.mine

import android.os.Bundle
import android.text.InputFilter
import android.view.View
import com.aifeng.base.BaseActivity
import com.aifeng.common_ui.helper.*
import com.aifeng.hassan_food.R
import com.aifeng.utils.bindLifeCycle
import com.aifeng.utils.getExtra
import com.aifeng.utils.subscribeByNoError
import com.aifeng.viewmodel.SettingViewModel
import kotlinx.android.synthetic.main.activity_info_modify.*
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class InfoModifyActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_info_modify)
        initTitle(
            title = when (intent.getExtra("type")) {
                "1" -> "修改昵称"
                "2" -> "修改店铺名称"
                else -> "修改名称"
            }
        )

        info_name.requestFocus()
    }

    override fun getViewModel() = getViewModel<SettingViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        super.initTitle(title, name, divider)
        info_name.filters = arrayOf<InputFilter>(NameLengthFilter(20))
        when (intent.getExtra("type")) {
            "1" -> {
                info_name.hint = "请输入昵称"
                info_name.setText(getString("nickName"))
                info_name.setSelection(info_name.text.length)
            }
            "2" -> {
                info_name.hint = "请输入店铺名称"
                info_name.setText(getString("shopName"))
                info_name.setSelection(info_name.text.length)
            }
        }
    }

    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_sure -> {
                when {
                    info_name.text.isBlank() -> {
                        info_name.requestFocus()
                        toast(
                            when (intent.getExtra("type")) {
                                "1" -> "请输入昵称"
                                "2" -> "请输入店铺名称"
                                else -> "请输入名称"
                            }
                        )
                        return
                    }
                }

                when (intent.getExtra("type")) {
                    "1" -> {
                        getViewModel().editInfo(
                            "userid" to getString("userId"),
                            "nick" to info_name.textString()
                        )
                            .bindLifeCycle(getLifecycleOwner())
                            .subscribeByNoError(
                                event = getViewModel(),
                                onSuccess = {
                                    putShared("nickName", info_name.textString())
                                    ActivityStack.screenManager.popActivities(this::class.java)
                                }
                            )
                    }
                    "2" -> {
                        getViewModel().editInfo(
                            "userid" to getString("userId"),
                            "shopname" to info_name.textString()
                        )
                            .bindLifeCycle(getLifecycleOwner())
                            .subscribeByNoError(
                                event = getViewModel(),
                                onSuccess = {
                                    putShared("shopName", info_name.textString())
                                    ActivityStack.screenManager.popActivities(this::class.java)
                                }
                            )
                    }
                }
            }
        }
    }

}
